﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using System;
using System.Collections.Generic;
using Safover.LidarCollector.Core;
using Safover.LidarCollector.Config;

namespace Ac.Bly
{
    internal class BlyValueConverter
    {
        public static int CumulativePulses => RuntimeConfigManager.Instance.AcquisitionCardConfig.CumulativePulses;

        public static LidarDataReceivedEventArgs Convert(byte[] bytes)
        {
            return RealConvert(bytes);
        }

        private static LidarDataReceivedEventArgs RealConvert(byte[] bytes) 
        {
            var packetLength = BitConverter.ToUInt16(bytes, 9);

            var cumulativePulses = BitConverter.ToUInt16(bytes, 15);
            var samplingDepth = BitConverter.ToUInt16(bytes, 17);
            var channelNumber = bytes[21];

            var values = new List<double>(packetLength / 4);

            for (int offset = 22; offset < packetLength - 1; offset += 4)
            {
                var original = BitConverter.ToUInt32(bytes, offset);
                values.Add(original);
            }

            return new LidarDataReceivedEventArgs
            {
                ChannelNumber = channelNumber,
                CumulativePulses = cumulativePulses,
                SamplingDepth = samplingDepth,
                Values = values.ToArray()
            };
        }
    }
}
